package everyday;

import java.util.Arrays;

/**
 * @author 兴趣使然黄小黄
 * @version 1.0
 * @date 2023/6/7 22:56
 * 2611. 老鼠和奶酪
 * https://leetcode.cn/problems/mice-and-cheese/
 */
public class Solution2611 {

    public int miceAndCheese(int[] reward1, int[] reward2, int k) {
        // 先将 n 个奶酪都分给第二只老鼠
        // 维护一个 diff 数组存储两只老鼠吃每块儿奶酪的得分差
        int n = reward1.length;
        int score = 0;
        Integer[] diff = new Integer[n];
        for (int i = 0; i < n; ++i) {
            score += reward2[i];
            diff[i] = reward1[i] - reward2[i];
        }
        // diff 进行排序, 贪心策略选出价值差最高的 k 个奶酪, 计算到 score 中即可
        Arrays.sort(diff, (a, b) -> (b.compareTo(a)));  // 从大到小排序
        for (int i = 0; i < k; ++i) {
            score += diff[i];
        }
        return score;
    }
}
